home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / diff_2_1.lha / diff-2.1 / ifdef.c < prev    next >
C/C++ Source or Header  |  1992-09-30  |  5KB  |  184 lines

  1. /* #ifdef-format output routines for GNU DIFF.
  2.    Copyright (C) 1989, 91, 92 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU DIFF General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU DIFF, but only under the conditions described in the
  15. GNU DIFF General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU DIFF so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "diff.h"
  23.  
  24. static void format_ifdef ();
  25. static void print_ifdef_hunk ();
  26. static void print_ifdef_lines ();
  27. struct change *find_change ();
  28.  
  29. static int next_line;
  30.  
  31. /* Print the edit-script SCRIPT as a merged #ifdef file.  */
  32.  
  33. void
  34. print_ifdef_script (script)
  35.      struct change *script;
  36. {
  37.   next_line = - files[0].prefix_lines;
  38.   print_script (script, find_change, print_ifdef_hunk);
  39.   if (next_line < files[0].valid_lines)
  40.     {
  41.       begin_output ();
  42.       format_ifdef (group_format[UNCHANGED], next_line, files[0].valid_lines,
  43.             0, -1);
  44.     }
  45. }
  46.  
  47. /* Print a hunk of an ifdef diff.
  48.    This is a contiguous portion of a complete edit script,
  49.    describing changes in consecutive lines.  */
  50.  
  51. static void
  52. print_ifdef_hunk (hunk)
  53.      struct change *hunk;
  54. {
  55.   int first0, last0, first1, last1, deletes, inserts;
  56.   const char *format;
  57.  
  58.   /* Determine range of line numbers involved in each file.  */
  59.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
  60.   if (inserts)
  61.     format = deletes ? group_format[CHANGED] : group_format[NEW];
  62.   else if (deletes)
  63.     format = group_format[OLD];
  64.   else
  65.     return;
  66.  
  67.   begin_output ();
  68.  
  69.   /* Print lines up to this change.  */
  70.   if (next_line < first0)
  71.     format_ifdef (group_format[UNCHANGED], next_line, first0, 0, -1);
  72.  
  73.   /* Print this change.  */
  74.   next_line = last0 + 1;
  75.   format_ifdef (format, first0, next_line, first1, last1 + 1);
  76. }
  77.  
  78. /* Print a set of lines according to FORMAT.
  79.    Lines BEG0 up to END0 are from the first file.
  80.    If END1 is -1, then the second file's lines are identical to the first;
  81.    otherwise, lines BEG1 up to END1 are from the second file.  */
  82.  
  83. static void
  84. format_ifdef (format, beg0, end0, beg1, end1)
  85.      const char *format;
  86.      int beg0, end0, beg1, end1;
  87. {
  88.   register FILE *out = outfile;
  89.   register char c;
  90.   register const char *f = format;
  91.  
  92.   while ((c = *f++) != 0)
  93.     {
  94.       if (c == '%')
  95.     switch ((c = *f++))
  96.       {
  97.       case 0:
  98.         return;
  99.  
  100.       case '<':
  101.         /* Print lines deleted from first file.  */
  102.         print_ifdef_lines (line_format[OLD], &files[0], beg0, end0);
  103.         continue;
  104.  
  105.       case '=':
  106.         /* Print common lines.  */
  107.         print_ifdef_lines (line_format[UNCHANGED], &files[0], beg0, end0);
  108.         continue;
  109.  
  110.       case '>':
  111.         /* Print lines inserted from second file.  */
  112.         if (end1 == -1)
  113.           print_ifdef_lines (line_format[NEW], &files[0], beg0, end0);
  114.         else
  115.           print_ifdef_lines (line_format[NEW], &files[1], beg1, end1);
  116.         continue;
  117.  
  118.       case '0':
  119.         c = 0;
  120.         break;
  121.  
  122.       default:
  123.         break;
  124.       }
  125.       putc (c, out);
  126.   }
  127. }
  128.  
  129. /* Use FORMAT to print each line of CURRENT starting with FROM
  130.    and continuing up to UPTO.  */
  131. static void
  132. print_ifdef_lines (format, current, from, upto)
  133.      const char *format;
  134.      const struct file_data *current;
  135.      int from, upto;
  136. {
  137.   const char * const *linbuf = current->linbuf;
  138.  
  139.   /* If possible, use a single fwrite; it's faster.  */
  140.   if (!tab_expand_flag && strcmp (format, "%l\n") == 0)
  141.     fwrite (linbuf[from], sizeof (char),
  142.         linbuf[upto] + (linbuf[upto][-1] != '\n') -  linbuf[from],
  143.         outfile);
  144.   else if (!tab_expand_flag && strcmp (format, "%L") == 0)
  145.     fwrite (linbuf[from], sizeof (char), linbuf[upto] -  linbuf[from], outfile);
  146.   else
  147.     for (;  from < upto;  from++)
  148.       {
  149.     register FILE *out = outfile;
  150.     register char c;
  151.     register const char *f = format;
  152.  
  153.     while ((c = *f++) != 0)
  154.       {
  155.         if (c == '%')
  156.           switch ((c = *f++))
  157.         {
  158.         case 0:
  159.           goto format_done;
  160.  
  161.         case 'l':
  162.           output_1_line (linbuf[from],
  163.                  linbuf[from + 1]
  164.                    - (linbuf[from + 1][-1] == '\n'), 0, 0);
  165.           continue;
  166.  
  167.         case 'L':
  168.           output_1_line (linbuf[from], linbuf[from + 1], 0, 0);
  169.           continue;
  170.  
  171.         case '0':
  172.           c = 0;
  173.           break;
  174.  
  175.         default:
  176.           break;
  177.         }
  178.         putc (c, out);
  179.       }
  180.  
  181.       format_done:;
  182.       }
  183. }
  184.